/*************************************************************************
 *  Compilation:  javac Stopwatch.java
 *	@author Johan von Tangen Sivertsen;
 *
 *************************************************************************/

/**
 *  <i>jStopwatch</i>. This class is an improved version of the stopwatch
 *  class provided with Algorithms 4 by Robert Sedgewick and Kevin Wayne.
 *  It's indented to provide better functionality while maintaining compatibility
 *  with the book examples.
 */



public class jStopwatch { 

    private long start;

   /**
     * Creates and starts stopwatch object. Runs the garbage collector first to reduce the risk of it running during timing.
     */
    public jStopwatch() {
    	System.gc();
        start = System.nanoTime();
    } 
    
    /**
     * Resets the timer.
     */
public void reset(){
	System.gc();
    start = System.nanoTime();
	
}

   /**
     * Return elapsed time (in seconds) since the timer was started.
     */
    public double elapsedTime() {
        long now = System.nanoTime();
        return (now - start) / 1000000000.0;
    }
    
    public String toString(){
    	return Double.toString(elapsedTime());
    }

} 
